{extend name="normal_base" /}
{block name="navigation"}{/block}
{block name="body"}
<div class="x-body">
    <form action="{:url('add')}" method="post" class="layui-form layui-form-pane" id="from_id">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" value="" required  lay-verify="required" placeholder="请输入角色名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属公司</label>
            <div class="layui-input-block">
                <select name="code" id="code" lay-filter="code">
                    <option value=''>请选择</option>
                    {foreach name='sign_company_list' item='vo'}
                    <option value="{$vo.code}">{$vo.sign_company}</option>
                    {/foreach}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked="checked">
                <input type="radio" name="status" value="0" title="禁用">
            </div>
        </div>

        <!--授权-->
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">
                拥有权限
            </label>
            <table  class="layui-table layui-input-block">
                <tbody>
                {foreach name="menu" item="vo"}
                {if condition="!empty($vo.children)"}
                <tr>
                    <td>
                        <input type="checkbox" name="" lay-skin="primary" title="{$vo.title}" class="p_menu" lay-filter="p_menu" id="{$vo.id}"/>
                    </td>
                    <td>
                        <div class="layui-input-block">
                            {if condition="isset($vo.children)"}
                                {foreach name="vo.children" item="children"}
                                <input name="rules[]" lay-skin="primary" type="checkbox" value="{$children.id}" title="{$children.title}" class="s_menu_{$vo.id}" />
                                {/foreach}
                            {/if}
                        </div>
                    </td>
                </tr>
                {/if}
                {/foreach}
                </tbody>
            </table>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="add">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
{/block}

{block name="script"}
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
                ,layer = layui.layer;

        //监听提交
        form.on('submit(add)', function(data){
            //发异步，把数据提交给php
            $("#from_id").ajaxSubmit({
                beforeSubmit:function () {
                    //表单提交前
                 },
                success:function (data) {
                    //console.log(JSON.stringify(data));
                    if (data.code == 1) {
                        layer.msg(data.msg,{icon:5,time:1500});
                    } else {
                        layer.alert("添加成功", {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            //刷新父层页面
                            parent.location.replace(parent.location.href);
                        });
                    }
                }
            });
            return false;
        });

        //监听父菜单的选中/取消选中，点击父类选中时，让子类全部跟着选中，取消时，子类全部跟着取消
        form.on('checkbox(p_menu)', function(data){
            //var id = $(data.elem).attr('id');
            var child = $(data.elem).parent('td').next('td').find('input[type="checkbox"]');
            child.each(function(index, item){
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });
    });
</script>
{/block}